'use strict';
const dayjs = require("dayjs");
const {getDefaultModelConfig} = require("../../utils/common");
const {formatDate} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, TEXT } = app.Sequelize;
    return app.model.define(
        'Article',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            title: {type: STRING(255), allowNull: false, comment: '标题'},
            author: {type: STRING(100), allowNull: false, comment: '作者'},
            content: {type: TEXT, allowNull: false, comment: '内容（富文本）'},
            summary: {type: STRING(500), comment: '摘要'},
            cover: {type: STRING(500), comment: '封面图片'},
            category_id: {type: INTEGER, comment: '分类ID'},
            tags: {type: STRING(500), comment: '标签，JSON格式存储'},
            view_count: {type: INTEGER, defaultValue: 0, comment: '浏览次数'},
            like_count: {type: INTEGER, defaultValue: 0, comment: '点赞次数'},
            sort: {type: INTEGER, defaultValue: 0, comment: '排序'},
            status: {type: TINYINT, defaultValue: 1, comment: '状态：1-发布，0-草稿'},
            is_top: {type: TINYINT, defaultValue: 0, comment: '是否置顶：1-是，0-否'},
            is_recommend: {type: TINYINT, defaultValue: 0, comment: '是否推荐：1-是，0-否'},
            publish_time: {
                type: DATE, comment: '发布时间',
                get() {
                    return formatDate(this.getDataValue('publish_time'), 'YYYY-MM-DD HH:mm:ss');
                },
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'article',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
};
